![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@types/koa-bodyparser
Advanced tools
TypeScript definitions for koa-bodyparser
@types/koa-bodyparser provides TypeScript definitions for the koa-bodyparser middleware, which is used to parse the body of HTTP requests in Koa applications.
Basic Usage
This code demonstrates the basic usage of koa-bodyparser. It sets up a Koa application, uses the bodyParser middleware to parse incoming request bodies, and then responds with the parsed body.
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.use(bodyParser());
app.use(async ctx => {
ctx.body = ctx.request.body;
});
app.listen(3000);
Custom Options
This code demonstrates how to use custom options with koa-bodyparser. It configures the middleware to parse JSON, form, and text bodies with specific size limits.
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.use(bodyParser({
enableTypes: ['json', 'form', 'text'],
jsonLimit: '1mb',
textLimit: '1mb',
formLimit: '1mb'
}));
app.use(async ctx => {
ctx.body = ctx.request.body;
});
app.listen(3000);
Error Handling
This code demonstrates how to handle errors in koa-bodyparser. It sets up a custom error handler that throws a 422 error if the body parsing fails.
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.use(bodyParser({
onerror: (err, ctx) => {
ctx.throw('body parse error', 422);
}
}));
app.use(async ctx => {
ctx.body = ctx.request.body;
});
app.listen(3000);
koa-body is a more comprehensive body parser middleware for Koa that supports multipart, urlencoded, and JSON request bodies. It offers more features compared to koa-bodyparser, such as file uploads and custom handling of multipart requests.
koa-multer is a middleware for handling multipart/form-data, which is primarily used for uploading files. It is based on multer and provides more advanced file handling capabilities compared to koa-bodyparser.
koa-json-body is a simpler alternative to koa-bodyparser that focuses solely on parsing JSON request bodies. It is lightweight and easy to use but lacks the flexibility of handling other content types.
npm install --save @types/koa-bodyparser
This package contains type definitions for koa-bodyparser (https://github.com/koajs/body-parser).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/koa-bodyparser.
/* =================== USAGE ===================
import bodyParser = require("koa-bodyparser");
var Koa = require('koa');
var app = new Koa();
app.use(bodyParser());
=============================================== */
import * as Koa from "koa";
declare module "koa" {
interface Request {
body?: unknown;
rawBody: string;
}
}
declare function bodyParser(opts?: bodyParser.Options): Koa.Middleware;
declare namespace bodyParser {
interface Options {
/**
* parser will only parse when request type hits enableTypes, default is ['json', 'form'].
*/
enableTypes?: string[] | undefined;
/**
* requested encoding. Default is utf-8 by co-body
*/
encoding?: string | undefined;
/**
* limit of the urlencoded body. If the body ends up being larger than this limit
* a 413 error code is returned. Default is 56kb
*/
formLimit?: string | undefined;
/**
* limit of the json body. Default is 1mb
*/
jsonLimit?: string | undefined;
/**
* limit of the text body. Default is 1mb.
*/
textLimit?: string | undefined;
/**
* limit of the xml body. Default is 1mb.
*/
xmlLimit?: string | undefined;
/**
* when set to true, JSON parser will only accept arrays and objects. Default is true
*/
strict?: boolean | undefined;
/**
* custom json request detect function. Default is null
*/
detectJSON?: ((ctx: Koa.Context) => boolean) | undefined;
/**
* support extend types
*/
extendTypes?: {
json?: string[] | string | undefined;
form?: string[] | string | undefined;
text?: string[] | string | undefined;
} | undefined;
/**
* support custom error handle
*/
onerror?: ((err: Error, ctx: Koa.Context) => void) | undefined;
}
}
export = bodyParser;
These definitions were written by Jerry Chin, Hiroshi Ioka, Alexi Maschas, and Pirasis Leelatanon.
FAQs
TypeScript definitions for koa-bodyparser
The npm package @types/koa-bodyparser receives a total of 261,824 weekly downloads. As such, @types/koa-bodyparser popularity was classified as popular.
We found that @types/koa-bodyparser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.